package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiniu.util.Auth;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import com.woniu.domain.*;
import com.woniu.service.CourseService;
import com.woniu.service.CoursewareService;
import com.woniu.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Date: 2021/6/8:09:21  <br/>
 * Description:课程
 * @author 刘家元
 */

@RestController
@RequestMapping("/course/")
public class CourseController {

    @Autowired(required = false)
    CourseService courseService;

    @Autowired
    UsersService usersService;
    @Autowired
    CoursewareService coursewareService;

    /**
     * 课程页面需要获取的集合
     * @param page
     * @param pageSize
     * @return
     */

    @RequestMapping("courseList")
    public PageInfo<Course> courseQuery(Integer page ,Integer pageSize){
        PageHelper.startPage(page,pageSize);
        List<Course> course = courseService.courseQuery();
        PageInfo<Course> pageInfo = new PageInfo<>(course);
        System.out.println("++++++++++++++++++++++++++"+pageInfo);
        return pageInfo;
    }

    /**
     * 学生首页需要获取的课程集合
     * @return
     */
    @RequestMapping("stuCourseList")
    public List<Course> courseQuery(){
        return courseService.courseQuery();
    }

    @RequestMapping("condition")
    public PageInfo<Course> courseConditionQuery(@RequestBody CourseManagementDTO<Course> courseManagementDTO){
        PageHelper.startPage(courseManagementDTO.getPage(),courseManagementDTO.getPageSize());
        List<Course> course = courseService.courseConditionQuery(courseManagementDTO.getObj());
        PageInfo<Course> pageInfo = new PageInfo<>(course);
        return pageInfo;
    }
    /*
     * Description : 根据id课程  <br/>
     * ChangeLog : 1. 创建 (2021/6/8 09:20 [刘家元]);
     ** @param null
     * *  @return null
    */

    @RequestMapping("del")
    public int courseDelById(Integer id){
        return courseService.courseDelById(id);
    }

    @RequestMapping("details")
    public Course courseGetById(Integer id){
        return courseService.courseGetById(id);
    }



    @RequestMapping("role")
    public List<User> getByRoleId(Integer roleId){
        return usersService.getByRoleId(roleId);
    }

    @RequestMapping("addCourse")
    public int addCourse(@RequestBody CourseAndChapters courseAndChapters){
        System.out.println("---------------------------------"+courseAndChapters);

        return courseService.addCourse(courseAndChapters);
//        return courseService.addCourse(course);
    }

    @RequestMapping("upCourse")
    public int updateCourse(@RequestBody Course course){
        System.out.println("---------------------------------"+course);
        return courseService.updateCourse(course);
    }

    @RequestMapping("getUpToken")
    public String getUpToken(){
        //最简单的上传凭证只需要AccessKey，SecretKey和Bucket就可以
        String accessKey = "Cx6qcSumdh6eFQGexCl0KQePStNj42kekeJaSMbS";
        String secretKey = "9F4gi3egANQX-E6qZdqLtWrna8LcrDyc0I7JfDM-";
        String bucket = "zengchenlin";
        Auth auth = Auth.create(accessKey, secretKey);
        //获得然后 把这个upToken传递给前端
        String upToken = auth.uploadToken(bucket);
        return upToken;
    }

    @RequestMapping("kejian")
    public List<Courseware> queryAllCourseware(){
        return coursewareService.coursewareQuery();
    }



    @GetMapping("/getCourseById")
    public Course getUserById(Integer id){

        return courseService.courseGetById(id);
    }
}
